﻿using System;

namespace UniRx.Operators
{
	// Token: 0x020002A8 RID: 680
	internal class ForEachAsyncObservable<T> : OperatorObservableBase<Unit>
	{
		// Token: 0x06000EE9 RID: 3817 RVA: 0x00045B74 File Offset: 0x00043F74
		public ForEachAsyncObservable(IObservable<T> source, Action<T> onNext) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.onNext = onNext;
		}

		// Token: 0x06000EEA RID: 3818 RVA: 0x00045B90 File Offset: 0x00043F90
		public ForEachAsyncObservable(IObservable<T> source, Action<T, int> onNext) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.onNextWithIndex = onNext;
		}

		// Token: 0x06000EEB RID: 3819 RVA: 0x00045BAC File Offset: 0x00043FAC
		protected override IDisposable SubscribeCore(IObserver<Unit> observer, IDisposable cancel)
		{
			if (this.onNext != null)
			{
				return this.source.Subscribe(new ForEachAsyncObservable<T>.ForEachAsync(this, observer, cancel));
			}
			return this.source.Subscribe(new ForEachAsyncObservable<T>.ForEachAsync_(this, observer, cancel));
		}

		// Token: 0x0400089F RID: 2207
		private readonly IObservable<T> source;

		// Token: 0x040008A0 RID: 2208
		private readonly Action<T> onNext;

		// Token: 0x040008A1 RID: 2209
		private readonly Action<T, int> onNextWithIndex;

		// Token: 0x020002A9 RID: 681
		private class ForEachAsync : OperatorObserverBase<T, Unit>
		{
			// Token: 0x06000EEC RID: 3820 RVA: 0x00045BE0 File Offset: 0x00043FE0
			public ForEachAsync(ForEachAsyncObservable<T> parent, IObserver<Unit> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000EED RID: 3821 RVA: 0x00045BF4 File Offset: 0x00043FF4
			public override void OnNext(T value)
			{
				try
				{
					this.parent.onNext(value);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x06000EEE RID: 3822 RVA: 0x00045C54 File Offset: 0x00044054
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000EEF RID: 3823 RVA: 0x00045C8C File Offset: 0x0004408C
			public override void OnCompleted()
			{
				this.observer.OnNext(Unit.Default);
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x040008A2 RID: 2210
			private readonly ForEachAsyncObservable<T> parent;
		}

		// Token: 0x020002AA RID: 682
		private class ForEachAsync_ : OperatorObserverBase<T, Unit>
		{
			// Token: 0x06000EF0 RID: 3824 RVA: 0x00045CD4 File Offset: 0x000440D4
			public ForEachAsync_(ForEachAsyncObservable<T> parent, IObserver<Unit> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000EF1 RID: 3825 RVA: 0x00045CE8 File Offset: 0x000440E8
			public override void OnNext(T value)
			{
				try
				{
					this.parent.onNextWithIndex(value, this.index++);
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x06000EF2 RID: 3826 RVA: 0x00045D58 File Offset: 0x00044158
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000EF3 RID: 3827 RVA: 0x00045D90 File Offset: 0x00044190
			public override void OnCompleted()
			{
				this.observer.OnNext(Unit.Default);
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x040008A3 RID: 2211
			private readonly ForEachAsyncObservable<T> parent;

			// Token: 0x040008A4 RID: 2212
			private int index;
		}
	}
}
